package com.info3k.client.commands.address;

import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl3D;
import com.google.gwt.maps.client.control.MapTypeControl;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Command;
import com.info3k.client.Info3K;
import com.info3k.client.components.GlassDialogBox;
import com.info3k.client.domain.Address;


public class ShowAddressCommand extends GlassDialogBox implements Command {

	private MapWidget map = new MapWidget(); 
	
	private Marker marker;
	
	public ShowAddressCommand(Address address){
		setText(address.getAddress());
		map.setSize("400px", "300px");
		map.addControl(new LargeMapControl3D());
		map.setCurrentMapType(MapType.getSatelliteMap());
		map.setCenter(LatLng.newInstance(address.getLat(),address.getLon()), 14);
		map.addControl(new MapTypeControl());
		marker = new Marker(LatLng.newInstance(address.getLat(),address.getLon()));
		map.addOverlay(marker);
		
		String contentText = Info3K.msgs.address() + ": " + address.getAddress() + "<br>";
		if(address.getHouseNumber().length()>0){
			contentText += Info3K.msgs.houseNumber() + ": " + address.getHouseNumber() + "<br>";
		}
		
		contentText += Info3K.msgs.creator() + ": " + address.getUser().getUsername() + "<br>";
		
		final InfoWindowContent con = new InfoWindowContent(contentText);
		
		marker.addMarkerClickHandler(new MarkerClickHandler(){
			public void onClick(MarkerClickEvent event) {
				map.getInfoWindow().open(marker.getLatLng(), con);
			}
		});
		
		map.getInfoWindow().open(marker.getLatLng(), con);
			
		content.add(map);
		
		footer.add(closeButton);
	}

	public void execute() {
		center();
	}
}